package com.xsomnus.open.db.shardingjdbc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xsomnus.open.db.shardingjdbc.entity.Order;
import com.xsomnus.open.db.shardingjdbc.service.IOrderService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/5/5 0005 10:46
 * 当你发现自己的才华撑不起野心时，就请安静下来学习吧
 */
@RestController
@AllArgsConstructor
@RequestMapping("/order")
public class OrderController {

    private final IOrderService iOrderService;

    @PostMapping
    public boolean save() {
        Collection<Order> goods = new ArrayList<>();
        for (int i = 10; i < 20; i++) {
            goods.add(new Order().setId((long) i).setUserId((long) (i + RandomUtils.nextInt(20))).setStatus("status" + (i % 5)));
        }
        return iOrderService.saveBatch(goods);
    }

    @GetMapping("/{id}")
    public Order getById(@PathVariable Long id, @RequestParam Long userId) {
        return iOrderService.getOne(new QueryWrapper<Order>().eq("user_id", userId).eq("id", id));
    }

    @GetMapping("/all")
    public Collection<Order> listAll() {
        return iOrderService.list();
    }

}
